package com.azusa.xbjy.dao;

import com.azusa.xbjy.entity.Article;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ArticleMapper {

    /**
     * 根据title查询文章信息
     * @param title
     * @return
     */
    @Select("SELECT " +
            " article.id,  " +
            " article.title,  " +
            " article.content,  " +
            " article.browse_count AS browseCount,  " +
            " article.publish_date AS publishDate,  " +
            " article.publish_real_name AS publishRealName,  " +
            " article.user_id " +
            "FROM " +
            " article WHERE title " +
            " LIKE CONCAT('%',#{title},'%')")
    List<Article> findByTitle(String title);

    /**
     * 查询userId的用户收藏的文章ids
     * @param userId
     * @return
     */
    @Select("SELECT a_id FROM favorite WHERE u_id=#{userId}")
    List<Integer> findUserCollect(int userId);

    /**
     * 查询收藏了articleId文章的用户ids
     * @param articleId
     * @return
     */
    @Select("SELECT u_id FROM favorite WHERE a_id=#{articleId}")
    List<Integer> findArticleCollect(int articleId);

    /**
     * 查询文章被收藏数
     * @param aid
     * @return
     */
    @Select("SELECT COUNT(*) FROM favorite WHERE a_id=#{aid}")
    int findArticleCount(int aid);

    /**
     * 修改文章浏览数
     * @param aid
     * @return
     */
    @Update("UPDATE `article` SET browse_count = browse_count+1 WHERE id=#{aid}")
    int updateBrowseCount(int aid);

    @Select("<script>"
            +"SELECT "
            + " article.id,  "
            + " article.title,  "
            + " article.content,  "
            + " article.browse_count AS browseCount,  "
            + " article.publish_date AS publishDate,  "
            + " article.publish_real_name AS publishRealName"
            + " FROM article "
            + " WHERE article.id IN "
            + "<foreach item='item' index='index' collection='aIds' open='(' separator=',' close=')'>"
            + "#{item}"
            + "</foreach>"
            + " AND article.title LIKE CONCAT('%',#{title},'%')"
            + "</script>")
    List<Article> selectCollect(List<Integer> aIds,String title);
}
